package open.witype.filetiper.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.DecimalFormat;

import open.witype.filetiper.fragment.file.adapter.FileType;
import open.witype.filetiper.utils.PinyinUtils;

/**
 * Created by Typer on 2015/5/6 0006.
 * 文件的基本类型
 */
public class FileItemBean {

    private String suffix;

    private String absName;

    private String size;

    private String fileName;

    private int fileType;

    private String abbre;

    private boolean isFile;

    private boolean isDirectory;

    private String path;

    private String remark;

    public FileItemBean(File file) {
        this.fileName = file.getName();
        this.absName = getAbsName(file);
        this.suffix = getSuffix(file);
        this.size = getFileSize(file);
        this.fileType = getFileType(file);
        this.abbre = getAbbre(file);
        this.isFile = file.isFile();
        this.isDirectory = file.isDirectory();
        this.path = file.getPath();
    }

    public String getSuffix(File file) {
        String fileName = file.getName().toUpperCase();
        int index = fileName.toUpperCase().lastIndexOf('.');
        if (index == -1)
            return "";
        String suffix = fileName.substring(index + 1);
        if (suffix.length() >= 5)
            suffix = suffix.substring(0, 5);
        return suffix;
    }

    public String getAbsName(File file) {
        if (file.isDirectory())
            return file.getName();
        String fileName = file.getName();
        int index = fileName.toUpperCase().lastIndexOf('.');
        if (index == -1)
            return fileName;
        return fileName.substring(0, index);
    }

    public String getFileSize(File file) {
        if (file.isDirectory())
            return "";
        double size;
        String formatSize;
        FileInputStream fis;
        try {
            fis = new FileInputStream(file);
            size = fis.available();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
            return "0B";
        }
        DecimalFormat df = new DecimalFormat("#.00");
        DecimalFormat roundDf = new DecimalFormat("#");
        if (size < 1024)
            formatSize = roundDf.format(size) + "B";
        else if (size < 1048576)
            formatSize = roundDf.format(size / 1024) + "KB";
        else if (size < 1073741824)
            formatSize = df.format(size / 1048576) + "MB";
        else
            formatSize = df.format(size / 1073741824) + "GB";
        df = null;
        roundDf = null;
        return formatSize;
    }

    public String getAbbre(){
        return abbre;
    }

    public String getAbbre(File file) {
        if (isFile())
            return "";
        String abbre;
        String fileName = file.getName();
        if (fileName.length() <= 2) {
            abbre = fileName;
        } else {
            abbre = fileName.substring(0, 2);
        }
        abbre = PinyinUtils.getFirstSpell(abbre).toLowerCase();
        abbre = abbre.replace(abbre.charAt(0), Character.toUpperCase(abbre.charAt(0)));
        return abbre;
    }

    public int getFileType(File file) {
        if (file.isFile()) {
            String fileName = file.getName();
            if (fileName.startsWith("."))
                return 0;
            int po;
            try {
                po = FileType.fromString(suffix);
            } catch (IllegalArgumentException e) {
                return 0;
            }
            return po;
        }
        return 1;
    }

    public boolean isFile() {
        return isFile;
    }

    public boolean isDirectory() {
        return isDirectory;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }

    public String getAbsName() {
        return absName;
    }

    public void setAbsName(String absName) {
        this.absName = absName;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public int getFileType() {
        return fileType;
    }

    public void setFileType(int fileType) {
        this.fileType = fileType;
    }

    public void setAbbre(String abbre) {
        this.abbre = abbre;
    }

    public String getPath() {
        return path;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remerk) {
        this.remark = remerk;
        isSetRemark = true;
    }

    private boolean isSetRemark;

    public boolean isSetRemark() {
        return isSetRemark;
    }

    public void setIsSetRemark(boolean isSetRemark) {
        this.isSetRemark = isSetRemark;
    }


}
